package com.test.uiapp2.function.Settings;

import android.os.RemoteException;
import android.provider.Contacts;
import android.util.Log;

import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import com.test.uiapp2.base.TestBase;
import com.test.uiapp2.element.Base.BaseElement;
import com.test.uiapp2.element.Settings.SettingsDisplayElement;
import com.test.uiapp2.element.Settings.SettingsWlanElement;

public class SettingsDisplayFunction extends TestBase {

    //屏幕超时-修改时间
    public void ScreenTimeoutUpdateTime(){
        try {
            sleep(WAIT);
            unLockScreen();
            sleep(WAIT);
            startSettings();
            if(getTest(SettingsDisplayElement.ADVANCED).exists()){
                clickByText(SettingsDisplayElement.ADVANCED);
                sleep(WAIT);
                verify("未展开高级设置界面",getTest(SettingsDisplayElement.SCREENTIMEOUT).exists());
                clickByText(SettingsDisplayElement.SCREENTIMEOUT);
                TimeoutSetting_30();
                sleep(WAIT);
                UiObject recycler_view = getId(BaseElement.SETTINGS, SettingsDisplayElement.RECYCLER_VIEW);
                UiObject Screen = recycler_view.getChild(new UiSelector().className(SettingsDisplayElement.LINEARLAYOUT).index(6));
                UiObject relativelayout = Screen.getChild(new UiSelector().className(SettingsDisplayElement.RELATIVELAYOUT).index(0));
                String TimeoutName = relativelayout.getChild(new UiSelector().resourceId(SettingsDisplayElement.SUMMARY).index(1)).getText();
                verify("未设置成功",TimeoutName.contains("30"));
            }else{
                verify("未进入显示设置界面",getTest(SettingsDisplayElement.ADVANCED).exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }



    //屏幕超时15秒
    public  void  ScreenTimeout(){
        try {
            startSettings();
            if(getTest(SettingsDisplayElement.ADVANCED).exists()){
                clickByText(SettingsDisplayElement.ADVANCED);
                sleep(WAIT);
                verify("未展开高级设置界面",getTest(SettingsDisplayElement.SCREENTIMEOUT).exists());
                clickByText(SettingsDisplayElement.SCREENTIMEOUT);
                TimeoutSetting_15();
                sleep(HWAIT*8);
                //isScreenOn== false  灭屏状态     isScreenOn== true    亮屏状态
                verify("未进入待机状态",!uiDevice.isScreenOn());
            }else{
                verify("未进入显示设置界面",getTest(SettingsDisplayElement.ADVANCED).exists());
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    //超时时间设置
    public  void TimeoutSetting_15(){
        try {
            sleep(WAIT*2);
            UiObject selectTime = getId(BaseElement.SETTINGS, SettingsDisplayElement.SELECT_DIALOG);
            UiObject child = selectTime.getChild(new UiSelector().className(SettingsDisplayElement.LINEARLAYOUT).index(0));
            child.click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }
    //超时时间设置
    public  void TimeoutSetting_30(){
        try {
            sleep(WAIT*2);
            UiObject selectTime = getId(BaseElement.SETTINGS, SettingsDisplayElement.SELECT_DIALOG);
            UiObject child = selectTime.getChild(new UiSelector().className(SettingsDisplayElement.LINEARLAYOUT).index(6));
            child.click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //打开设置-显示公用方法
    public void startSettings(){
        try {
            startApp(SettingsWlanElement.WLANNAME);
            sleep(WAITTIME);
            uiDevice.swipe(576,463,556,1146,STEP);
            if(getTest(SettingsDisplayElement.SHOW).exists()){
                clickByText(SettingsDisplayElement.SHOW);
                sleep(WAIT);
            }else{
                verify("未进入设置界面",getTest(SettingsDisplayElement.SHOW).exists());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
